//
//  SportsDetailViewController.m
//  WisdomZhuHai
//
//  Created by JasonLee on 15/1/15.
//  Copyright (c) 2015年 wyd. All rights reserved.
//

#import "SportsDetailViewController.h"
#import "Common.h"
#import "TransforController.h"
#import "GoodDetailViewController.h"
#import "OrderViewController.h"
#import "BottomBarViewController.h"
#import "CommentViewController.h"
#import "ProductListViewController.h"
#import "BaiduMapViewController.h"
#import "PBSafariActivity.h"
#import "PBWebViewController.h"

#define NAME_CONST @"name"
#define TOPIMAGE_CONST @"logo_path"
#define ADDRESS_CONST @"address"
#define DURATION_CONST @"open_time"
#define PHONE_CONST @"phone"
#define TYPE_CONST @"type_name"
#define INTRO_CONST @"small_intro"
#define GOTOMOREINTRO @"intro_url"
#define HEADPHOTO_CONST @"user_img"
#define USERNAME_CONST @"username"
#define DATETIME_CONST @"created_at"
#define COMMENT_CONST @"content"
#define LIEKCOUNT_CONST @"good"
#define IMGPATH_CONST @"img_path"
#define PHOTO_CONST @"photo"
#define TITLE_CONST @"title"
#define PRICE_CONST @"price"
#define DIC_COMMENT_CONST @"comments"
#define DIC_EXTRA_CONST @"goods"
#define DIC_NEARBY_CONST @"nearby"

@interface SportsDetailViewController ()<UITableViewDataSource, UITableViewDelegate>{
    UIView *_headerView;
    NSMutableDictionary *dic_dataOfTop;
    UIButton *btn_gotoMap;
    UIButton *btn_gotoOK;
    UIButton *btn_gotoDetail;
    UILabel *lb_address;
    UILabel *lb_duration;
    UILabel *lb_phone;
    UITextField *lb_type;
    UILabel *lb_intro;
    UIImageView *view_head1;
    UILabel *lb_titleOfHead;
    UILabel *lb_titleOfHeadDefault;
}

@property(nonatomic, strong) NSArray *array_comments;
@property(nonatomic, strong) NSArray *array_extra;
@property(nonatomic, strong) NSArray *array_nearby;
@property(nonatomic, strong) UITableView *tableView;
@property(nonatomic, strong) UIView *view_head;
//@property(nonatomic, strong) NSArray *array_img;
@property(nonatomic, strong) BottomBarViewController *bottomBarController;
@property (nonatomic, strong) PBWebViewController *webViewController;
@end

@implementation SportsDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initData];
    [self initView];
    [self.view setFrame:CGRectMake(0, 0, 320, [UIScreen mainScreen].bounds.size.height)];
//    self.array_img = @[[UIImage imageNamed:@"Guide/分享图标.png"], [UIImage imageNamed:@"Guide/收藏图标.png"], [UIImage imageNamed:@"Guide/评论图标.png"],[UIImage imageNamed:@"Guide/点赞图标.png"]];
    
    _tableView = [[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height - HEIGHT_BOTTOM_BAR) style:UITableViewStyleGrouped]retain];
    _tableView.tableHeaderView = _headerView;
    _tableView.tableFooterView = [[UIView alloc]init];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    UIView *view_statusBar = [[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 20)] retain];
    view_statusBar.backgroundColor = [Common colorWithRGB:0x18aaf6];
    [self.view addSubview:view_statusBar];
    self.view_head = [[TransforController shardeInstanceOfNavigationBarCoverViewWithY:20]retain];
    lb_titleOfHead = [[[UILabel alloc]initWithFrame:CGRectMake(60, 0, 200, 45)]retain];
    lb_titleOfHead.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:18.0];
    lb_titleOfHead.textColor = [UIColor whiteColor];
    lb_titleOfHead.textAlignment = UITextAlignmentCenter;
    [self.view_head addSubview:lb_titleOfHead];
    [self initToolbar];
    [self.view addSubview:self.view_head];
    
    
//    self.toolbar = [TransforController getToolBarWithColor:[TransforController getColorWithRed:225 andGreen:225 andBlue:225 andAlpha:1]];
    
//    [self.navigationController setToolbarHidden:NO animated:YES];
//    [self.view addSubview:self.toolbar];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



//- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
////    NSLog(@"视图滚动中y轴坐标%f",_tableView.contentOffset.y);
//    if(self.tableView == nil || self.view_head == nil){
//        return;
//    }
//    if(_tableView.contentOffset.y > 5 && _tableView.contentOffset.y < 150){
//        [self.view_head setAlpha:_tableView.contentOffset.y / 80];
////        [view_head setAlpha:0];
//    }
////    if(_tableView.contentOffset.y > 0 && _tableView.contentOffset.y < 100){
////        [lb_titleOfHeadDefault setAlpha: (100 - _tableView.contentOffset.y) / 100];
////    }
//}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)initView{
    NSArray *view_nib = [[NSBundle mainBundle]loadNibNamed:@"SportsView" owner:nil options:nil];
    _headerView = [view_nib objectAtIndex:0];
    [_headerView setFrame:CGRectMake(0, 0, 320, 420)];
    view_head1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 160)];
    [_headerView addSubview:view_head1];
    lb_address = (UILabel *)[_headerView viewWithTag:80001];
    lb_duration = (UILabel *)[_headerView viewWithTag:80002];
    lb_phone = (UILabel *)[_headerView viewWithTag:80003];
    lb_type = (UITextField *)[_headerView viewWithTag:80004];
    lb_type.textColor = [TransforController getColorWithRed:225 andGreen:225 andBlue:225 andAlpha:1];
    lb_intro = (UILabel *)[_headerView viewWithTag:80005];
    btn_gotoDetail = (UIButton *)[_headerView viewWithTag:80006];
    btn_gotoMap = (UIButton *)[_headerView viewWithTag:80007];
    [btn_gotoMap setAlpha:0];
    btn_gotoOK = (UIButton *)[_headerView viewWithTag:80008];
}

- (void)initToolbar{
//    self.view_beneath = [TransforController shardeInstanceOfNavigationBarCoverViewWithY:568-45];
//    self.view_beneath.backgroundColor = [TransforController getColorWithRed:210 andGreen:210 andBlue:210 andAlpha:1];
//    [self.view addSubview:self.view_beneath];
//    for(int i = 0; i < 4; i++){
//        UIImageView *img_temp = [[UIImageView alloc]initWithFrame:CGRectMake(i * (320 / 4) + 25, 8, 25, 25)];
//        UIButton *btn_temp = [[UIButton alloc]initWithFrame:CGRectMake(i * (320 / 4), 5, (320 / 4), 45)];
//        img_temp.image = [self.array_img objectAtIndex:i];
//        [img_temp setContentMode:UIViewContentModeScaleToFill];
//        [btn_temp setTag:200000 + i];
//        [btn_temp addTarget:self action:@selector(doBtnClickAction:) forControlEvents:UIControlEventTouchUpInside];
//        [self.view_beneath addSubview:img_temp];
//        [self.view_beneath addSubview:btn_temp];
//    }
    self.bottomBarController = [[BottomBarViewController alloc]initWithFrame:CGRectMake(0, self.view.bounds.size.height - HEIGHT_BOTTOM_BAR, 320, 45)];
    NSLog(@"%s || height of view:%f and screen:%f and bottomBar's position: %f, %f", __FUNCTION__, self.view.bounds.size.height, [UIScreen mainScreen].bounds.size.height, self.bottomBarController.view.frame.origin.x, self.bottomBarController.view.frame.origin.y);
    self.bottomBarController.bottomBarNavigationController = self.navigationController;
    [self.view addSubview:self.bottomBarController.view];
}

- (void)viewReloadData{
    if(dic_dataOfTop != nil){
        
        NSArray *array_temp = [NSArray arrayWithArray:[Common valueForKey:@"extra" dic:dic_dataOfTop]];
        NSMutableString *string_temp_total = [[NSMutableString alloc]init];
        for(int i = 0; i < [array_temp count]; i++){
            NSString *string_temp = [NSString stringWithFormat:@"%@:%@ \n", [array_temp[i] objectForKey:@"name"], [array_temp[i] objectForKey:@"value"]];
            [string_temp_total appendString:string_temp];
//            NSLog(@"%s || string_temp:%@, string_temp_total:%@", __FUNCTION__, string_temp, string_temp_total);
        }
        lb_type.text = string_temp_total;
        
        NSString *imgUrl = [Common valueForKey:TOPIMAGE_CONST dic:dic_dataOfTop];
        [view_head1 setImageWithURL:[Common getFullDownloadPathByRelativePath:imgUrl]];
        lb_address.text = [dic_dataOfTop objectForKey:ADDRESS_CONST];
        lb_duration.text = [dic_dataOfTop objectForKey:DURATION_CONST];
        lb_phone.text = [dic_dataOfTop objectForKey:PHONE_CONST];
        lb_phone.accessibilityValue = [dic_dataOfTop objectForKey:PHONE_CONST];
        
        UITapGestureRecognizer *ontap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gotoBaiduMap:)];
        [lb_address addGestureRecognizer:ontap];
        UITapGestureRecognizer *onCall = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(callNumber:)];
        [lb_phone addGestureRecognizer:onCall];
        
        lb_intro.text = [dic_dataOfTop objectForKey:INTRO_CONST];
        lb_titleOfHead.text = [dic_dataOfTop valueForKey:NAME_CONST];
        UIImageView *view_image = (UIImageView *)[_headerView viewWithTag:99999];
        int judgement = [[Common valueForKey:@"has_micro_site" dic:dic_dataOfTop] intValue];
        if(judgement == 1){
//            [btn_gotoOK setImage:[UIImage imageNamed:@"OK官网Gray.png"] forState:];
//            btn_gotoOK.imageView.image = [UIImage imageNamed:@"ok官网.png"];
            view_image.image = [UIImage imageNamed:@"ok官网.png"];
        }else{
//            btn_gotoOK.imageView.image = [UIImage imageNamed:@"ok官网Gray.png"];
        }
        [btn_gotoOK addTarget:self action:@selector(gotoWebOfHomePageWithUrl) forControlEvents:UIControlEventTouchUpInside];
        NSString *path = [Common valueForKey:GOTOMOREINTRO dic:dic_dataOfTop];
        if([path isEqualToString:@""]){
            btn_gotoDetail.titleLabel.textColor = [UIColor grayColor];
        }else{
            [btn_gotoDetail addTarget:self action:@selector(gotoWebOfMoreIntroWithUrl) forControlEvents:UIControlEventTouchUpInside];
        }
    }
}

- (void)initData{
//    [self getSportsDetailCallBack:dic];
//    target_id = 1;
    [self getSportsDetail];
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (section) {
        case 1:
            return [self.array_comments count] + 2;
            break;
        case 0:
            return [self.array_extra count] + 2;
            break;
        case 2:
            return [self.array_nearby count] + 1;
            break;
        default:
            return 0;
            break;
    }
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.section) {
        case 1:
            if(indexPath.row == 0){
                return 30;
            }else if(indexPath.row == ([self.array_comments count] + 1)){
                return 45;
            }else{
                return 80;
            }
            break;
        case 0:
            if(indexPath.row == 0){
                return 30;
            }else if(indexPath.row == ([self.array_extra count] + 1)){
                return 45;
            }else{
                return 80;
            }
            break;
        case 2:
            if(indexPath.row == 0){
                return 30;
            }else if(indexPath.row == ([self.array_nearby count] + 9999)){
                return 45;
            }else{
                return 80;
            }
            break;
        default:
            return 0;
            break;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = nil;
    NSArray *nibView = [[NSBundle mainBundle]loadNibNamed:@"SportsCell" owner:self options:nil];
//    NSLog(@"%s || indexPath.section:%d, indexPath.row:%d", __FUNCTION__, indexPath.section, indexPath.row);
    switch (indexPath.section) {
        case 1:
            if(indexPath.row == 0){
                cell = [tableView dequeueReusableCellWithIdentifier:@"HotComments"];
                if(cell == nil){
                    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"HotCommentsTop"];
                    cell.backgroundColor = [UIColor colorWithRed:225/255.0 green:225/255.0 blue:225/255.0 alpha:1];
                    cell.textLabel.text = @"热门评论";
//                    cell.detailTextLabel.text = @"更多";
//                    cell.detailTextLabel.font = [UIFont fontWithName:@"Arial" size:12.0];
//                    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                }
            }else if(indexPath.row ==  ([self.array_comments count] + 1)){
                cell = [tableView dequeueReusableCellWithIdentifier:@"HotComments"];
                if(cell == nil){
                    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HotCommentsBottom"];
                    if([self.array_comments count] < 1){
                        UILabel *btn_gotoMoreComments = [[UILabel alloc] initWithFrame:CGRectMake(125, 5, 100, 25)];
                        btn_gotoMoreComments.text = @"暂无更多";
                        btn_gotoMoreComments.textColor = [UIColor colorWithRed:215/255.0 green:215/255.0 blue:215/255.0 alpha:1];
                        btn_gotoMoreComments.font = [UIFont fontWithName:@"Arial" size:14.0];
                        [cell addSubview:btn_gotoMoreComments];
                    }else{
                        UILabel *btn_gotoMoreComments = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];
                        btn_gotoMoreComments.textAlignment = UITextAlignmentCenter;
                        btn_gotoMoreComments.text = @"查看更多评论";
                        btn_gotoMoreComments.textColor = [UIColor colorWithRed:110/255.0 green:180/255.0 blue:240/255.0 alpha:1];
                        btn_gotoMoreComments.font = [UIFont fontWithName:@"Arial" size:14.0];
                        [cell addSubview:btn_gotoMoreComments];
                    }
                }
            }else{
                cell = [tableView dequeueReusableCellWithIdentifier:@"HotComments"];
                if(cell == nil){
                    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HotCommentsMiddle"];
                    cell = [nibView objectAtIndex:0	];
                }
                if([self.array_comments count] >= 1){
//                    NSLog(@"%s || indexPath.row: %d", __FUNCTION__, indexPath.row);
                    NSDictionary *dic_temp = _array_comments[indexPath.row - 1];
                    NSString *imgUrl = [Common valueForKey:HEADPHOTO_CONST dic:dic_temp];
                    if(![imgUrl isEqualToString:@""]){
                        UIImageView *img_head = [(UIImageView *)[cell viewWithTag:70001] retain];
                        [img_head setImageWithURL:[Common getFullDownloadPathByRelativePath:imgUrl]];
                    }
                    UILabel *lb_username = (UILabel *)[cell viewWithTag:70002];
                    lb_username.text = [dic_temp objectForKey:USERNAME_CONST];
                    UILabel *lb_datetime = (UILabel *)[cell viewWithTag:70003];
                    lb_datetime.text = [dic_temp objectForKey:DATETIME_CONST];
                    UILabel *lb_comment = (UILabel *)[cell viewWithTag:70004];
                    lb_comment.text = [dic_temp objectForKey:COMMENT_CONST];
                    UIButton *btn_likeCount = (UIButton *)[cell viewWithTag:70005];
                    btn_likeCount.titleLabel.text = [dic_temp objectForKey:LIEKCOUNT_CONST];
                    [btn_likeCount setTitle:[dic_temp objectForKey:LIEKCOUNT_CONST] forState:UIControlStateNormal];
                }
            }
            break;
        case 0:
            if(indexPath.row == 0){
                cell = [tableView dequeueReusableCellWithIdentifier:@"Recommends"];
                if(cell == nil){
                    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"RecommendsTop"];
                    cell.backgroundColor = [UIColor colorWithRed:225/255.0 green:225/255.0 blue:225/255.0 alpha:1];
                    cell.textLabel.text = @"推荐";
//                    cell.detailTextLabel.text = @"更多";
//                    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//                    cell.detailTextLabel.font = [UIFont fontWithName:@"Arial" size:12.0];
                }
            }else if(indexPath.row == ([self.array_extra count] + 1)){
                cell = [tableView dequeueReusableCellWithIdentifier:@"Recommends"];
                if(cell == nil){
                    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"RecommendsBottom"];
                    if([self.array_extra count] < 1){
                        UILabel *btn_gotoMoreComments = [[UILabel alloc] initWithFrame:CGRectMake(125, 5, 100, 25)];
                        btn_gotoMoreComments.text = @"暂无更多";
                        btn_gotoMoreComments.textColor = [UIColor colorWithRed:215/255.0 green:215/255.0 blue:215/255.0 alpha:1];
                        btn_gotoMoreComments.font = [UIFont fontWithName:@"Arial" size:14.0];
                        [cell addSubview:btn_gotoMoreComments];
                    }else{
                        UILabel *btn_gotoMoreComments = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];
                        btn_gotoMoreComments.textAlignment = UITextAlignmentCenter;
                        btn_gotoMoreComments.text = @"查看更多";
                        btn_gotoMoreComments.textColor = [UIColor colorWithRed:110/255.0 green:180/255.0 blue:240/255.0 alpha:1];
                        btn_gotoMoreComments.font = [UIFont fontWithName:@"Arial" size:14.0];
                        [cell addSubview:btn_gotoMoreComments];
                    }
                }
            }else{
                cell = [tableView dequeueReusableCellWithIdentifier:@"Recommends"];
                if(cell == nil){
                    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"RecommendsMiddle"];
                    cell = [nibView objectAtIndex:1];
                }
                if([self.array_extra count] >= 1){
                    //                    NSLog(@"%s || indexPath.row: %d", __FUNCTION__, indexPath.row);
                    NSDictionary *dic_temp = self.array_extra[indexPath.row - 1];
                    NSString *imgUrl = [Common valueForKey:PHOTO_CONST dic:dic_temp];
                    UIImageView *img_head = [(UIImageView *)[cell viewWithTag:70010]retain];
                    [img_head setImageWithURL:[Common getFullDownloadPathByRelativePath:imgUrl]];
                    UILabel *lb_username = (UILabel *)[cell viewWithTag:70011];
                    lb_username.text = [dic_temp objectForKey:NAME_CONST];
                    UILabel *lb_price = (UILabel *)[cell viewWithTag:70012];
                    int type = [[Common valueForKey:@"type_id" dic:dic_dataOfTop] intValue];
                    NSString *string_temp = nil;
                    float price = [[Common valueForKey:P_PRICE dic:dic_temp] floatValue];
                    float prePrice = [[Common valueForKey:P_PREFERENTIAL_PRICE dic:dic_temp] floatValue];
                    if (prePrice >= 0 && prePrice < price) {
                        price = prePrice;
                    }
                    if(type == 10){
                        string_temp = [NSString stringWithFormat:@"%.2f元 / 小时", price];
                    }else{
                        string_temp = [NSString stringWithFormat:@"%.2f元", price];
                    }
                    lb_price.text = string_temp;
                    UIButton *btn_gotoBuy = (UIButton *)[cell viewWithTag:70013];
                    NSString *payment_mode = [Common valueForKey:P_PAYMENT dic:dic_temp];
                    if ([payment_mode isEqualToString:@"0"]) {
                        btn_gotoBuy.backgroundColor = [Common colorWithRGB:0xf8b62d];
                    } else {
                        [btn_gotoBuy setTitle:@"立即预约" forState:UIControlStateNormal];
                    }
                    if([[Common valueForKey:@"is_sell" dic:dic_temp]isEqualToString:@"0"]){
                        btn_gotoBuy.enabled = NO;
                        btn_gotoBuy.backgroundColor = [UIColor grayColor];
                    }
                    [btn_gotoBuy setTag:6000000 + (indexPath.row - 1)];
                    [btn_gotoBuy addTarget:self action:@selector(gotoGoods:) forControlEvents:UIControlEventTouchUpInside];
                }
            }
            break;
        case 2:
            if(indexPath.row == 0){
                cell = [tableView dequeueReusableCellWithIdentifier:@"FoodsRecommends"];
                if(cell == nil){
                    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"FoodsRecommendsTop"];
                    cell.backgroundColor = [UIColor colorWithRed:225/255.0 green:225/255.0 blue:225/255.0 alpha:1];
                    cell.textLabel.text = @"附近商街美食推荐";
//                    cell.detailTextLabel.text = @"更多";
//                    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//                    cell.detailTextLabel.font = [UIFont fontWithName:@"Arial" size:12.0];
                }
            }else if(indexPath.row == ([self.array_nearby count] + 9999)){
                cell = [tableView dequeueReusableCellWithIdentifier:@"FoodsRecommends"];
                if(cell == nil){
                    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"FoodsRecommendsBottom"];
                    if([self.array_nearby count] < 1){
                        UILabel *btn_gotoMoreComments = [[UILabel alloc] initWithFrame:CGRectMake(125, 5, 100, 25)];
                        btn_gotoMoreComments.text = @"暂无更多";
                        btn_gotoMoreComments.textColor = [UIColor colorWithRed:215/255.0 green:215/255.0 blue:215/255.0 alpha:1];
                        btn_gotoMoreComments.font = [UIFont fontWithName:@"Arial" size:14.0];
                        [cell addSubview:btn_gotoMoreComments];
                    }else{
                        UILabel *btn_gotoMoreComments = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];
                        btn_gotoMoreComments.textAlignment = UITextAlignmentCenter;
                        btn_gotoMoreComments.text = @"查看更多";
                        btn_gotoMoreComments.textColor = [UIColor colorWithRed:110/255.0 green:180/255.0 blue:240/255.0 alpha:1];
                        btn_gotoMoreComments.font = [UIFont fontWithName:@"Arial" size:14.0];
                        [cell addSubview:btn_gotoMoreComments];
                    }
                }
            }else{
                cell = [tableView dequeueReusableCellWithIdentifier:@"CommonCell"];
                // Configure the cell...
                if(cell == nil)
                {
                    cell = [[NSBundle mainBundle] loadNibNamed:@"CommonCell"  owner:self options:nil][0];
                }
                [self cellSetup:cell index:indexPath.row - 1];
            }
            break;
        default:
            break;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSIndexPath* indexPath1 = [_tableView indexPathForSelectedRow];
    NSLog(@"%s || indexPath.section:%d ,indexPath.row: %d", __FUNCTION__, indexPath1.section, indexPath1.row);
    switch (indexPath1.section) {
        case 1:
            if(indexPath1.row == 0){
            
            }else if(indexPath1.row == ([self.array_comments count] + 1)){
                [self gotoMoreComments];
            }else{
                
            }
            break;
        case 0:
            if(indexPath1.row == 0){
                
            }else if(indexPath1.row == ([self.array_extra count] + 1)){
                [self gotoMoreGoodsRecommends];
            }else{
                NSDictionary *dic_temp = self.array_extra[indexPath1.row - 1];
                int target_id = [[Common valueForKey:@"id" dic:dic_temp] intValue];
                [self gotoGoodsWithId:target_id];
            }
            break;
        case 2:
            if(indexPath1.row == 0){
                
            }else if(indexPath1.row == ([self.array_nearby count] + 1)){
                
            }else{
                
                if([self.array_nearby count] > 1){
                    NSDictionary *dic_temp = self.array_nearby[indexPath1.row - 1];
                    NSLog(@"%s || jumpToDetail", __FUNCTION__);
                    [self gotoNearbyRecommendsWithTargetId:[[Common valueForKey:@"id" dic:dic_temp] intValue]];
                }
            }
            break;
        default:
            break;
    }
}

- (void)getSportsDetail{
    NSLog(@"%s || target_id:%d", __FUNCTION__, self.target_id);
    [[ProtocolManager shareInstance]getStoreDetail:self.target_id sendType:@"V2" target:self selector:@selector(getSportsDetailCallBack:)];
}

- (void)getSportsDetailCallBack:(NSDictionary *)dic{
    NSLog(@"%s || dic:%@",__FUNCTION__, dic);
    if(dic == nil){
        return;
    }
//    NSDictionary *dic_temp = [Common valueForKey:@"store" dic:dic];
    if([[Common valueForKey:@"respCode" dic:dic] isEqualToString:@"000" ]){
        dic_dataOfTop = [[NSMutableDictionary alloc]initWithDictionary:dic copyItems:YES];
        [dic_dataOfTop removeObjectForKey:DIC_COMMENT_CONST];
        [dic_dataOfTop removeObjectForKey:DIC_EXTRA_CONST];
        [dic_dataOfTop removeObjectForKey:DIC_NEARBY_CONST];
        self.array_comments = [NSArray arrayWithArray:[Common valueForKey:DIC_COMMENT_CONST dic:dic]];
        self.array_extra = [NSArray arrayWithArray:[Common valueForKey:DIC_EXTRA_CONST dic:dic]];
        self.array_nearby = [NSArray arrayWithArray:[Common valueForKey:DIC_NEARBY_CONST dic:dic]];
        [_tableView reloadData];
        [self viewReloadData];
//        self.isClickable = true;
        if([[Common valueForKey:@"id" dic:dic_dataOfTop] length] > 0){
            [self.bottomBarController setEnabledWithData:[NSMutableDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"STORE",[Common valueForKey:@"id" dic:dic_dataOfTop], nil] forKeys:[NSArray arrayWithObjects:@"type",@"id", nil]]];
        }
    }
    
}

-(void)gotoWebOfHomePageWithUrl{
    NSString *url = [Common valueForKey:@"micro_website" dic:dic_dataOfTop];
//    WebIOS *web = [[WebIOS alloc]initWithFrame:[Common shareInstance].contentFrameNoTabBar url:[Common getUrlByString:url]];
//    //        web.title = [Common valueForKey:P_BRIEFINTRO dic:dic];
//    web.hidesBottomBarWhenPushed = YES;
//    [self.navigationController pushViewController:web animated:YES];
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        self.webViewController = [[PBWebViewController alloc] init];
        UIView *view_navigationBar = [TransforController shardeInstanceOfNavigationBarAndStatusBarCoverViewWithY:0];
        [self.webViewController.view addSubview:view_navigationBar];
    }
    
    PBSafariActivity *activity = [[PBSafariActivity alloc] init];
    self.webViewController.URL = [NSURL URLWithString:url];
    self.webViewController.applicationActivities = @[activity];
    self.webViewController.excludedActivityTypes = @[UIActivityTypeMail, UIActivityTypeMessage, UIActivityTypePostToWeibo];
    
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        self.webViewController.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:self.webViewController animated:YES];
    } else {
        [self.webViewController load];
    }
}

-(void)gotoWebOfMoreIntroWithUrl{
    NSString *url = [Common valueForKey:GOTOMOREINTRO dic:dic_dataOfTop];
    NSString *finalUrl = [NSString stringWithFormat:@"%@%@",@"http://",url];
    WebIOS *web = [[WebIOS alloc]initWithFrame:[Common shareInstance].contentFrameNoTabBar url:[Common getUrlByString:finalUrl]];
    //        web.title = [Common valueForKey:P_BRIEFINTRO dic:dic];
    web.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:web animated:YES];
}

- (void)gotoMoreComments{
    CommentViewController *commentViewController = [[CommentViewController alloc]init];
    commentViewController.hidesBottomBarWhenPushed = YES;
    commentViewController.target_id = [[Common valueForKey:@"id" dic:dic_dataOfTop] intValue];
    commentViewController.type = @"STORE";
    [self.navigationController pushViewController:commentViewController animated:YES];
}

- (void)gotoGoods:(id)sender{
    UIButton *btn = (UIButton *)sender;
    int target = btn.tag - 6000000;
    OrderViewController *orderViewCtrl = [[OrderViewController alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    NSDictionary *dic_temp = self.array_extra[target];
    orderViewCtrl.storeId = [[Common valueForKey:@"id" dic:dic_temp]intValue];
    orderViewCtrl.goodsInfo = [Common valueForKey:@"name" dic:dic_temp];
    orderViewCtrl.payment_mode = [Common valueForKey:P_PAYMENT dic:dic_temp];
//    orderViewCtrl.storeInfo = self.storeInfo;
//    NSDictionary *cellData = self.goodDetail;
    float price = [[Common valueForKey:P_PRICE dic:dic_temp] floatValue];
    float prePrice = [[Common valueForKey:P_PREFERENTIAL_PRICE dic:dic_temp] floatValue];
    if (prePrice >= 0.00 && prePrice < price) {
        price = prePrice;
    }
    orderViewCtrl.product_name = [Common valueForKey:P_NAME dic:dic_temp];
    orderViewCtrl.product_price = [NSString stringWithFormat:@"%.2f",price];
    orderViewCtrl.goodsId = [Common valueForKey:P_ID dic:dic_temp];
    orderViewCtrl.payment_mode = [NSString stringWithFormat:@"%@",[Common valueForKey:P_PAYMENT dic:dic_temp]];
    [self.navigationController pushViewController:orderViewCtrl animated:YES];
}

- (void)gotoGoodsWithId:(int)target_id{
    GoodDetailViewController *goodDetailViewController = [[GoodDetailViewController alloc]init];
    goodDetailViewController.goodId = target_id;
    goodDetailViewController.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController pushViewController:goodDetailViewController animated:YES];
}

- (void)gotoMoreGoodsRecommends{
    ProductListViewController *productListController = [[ProductListViewController alloc]init];
    productListController.hidesBottomBarWhenPushed = YES;
    productListController.target_id = self.target_id;
    [self.navigationController pushViewController:productListController animated:YES];
}

- (void)gotoNearbyRecommendsWithTargetId:(int)target{
    SportsDetailViewController *controller_target = [[SportsDetailViewController alloc]init];
    controller_target.target_id = target;
    controller_target.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:controller_target animated:YES];
}

- (void)gotoMoreNearbyRecommends{
    
}

- (void)gotoBaiduMap:(UITapGestureRecognizer*)onTap {
    NSLog(@"baiduMap");
    NSString* gps = [Common valueForKey:@"gps" dic:dic_dataOfTop];
    BaiduMapViewController *baiduMapCtrl = [[BaiduMapViewController alloc] init];
    baiduMapCtrl.gps = gps;
    baiduMapCtrl.name = [dic_dataOfTop valueForKey:NAME_CONST];
    [self.navigationController pushViewController:baiduMapCtrl animated:YES];
}

- (void)callNumber:(UITapGestureRecognizer*)onCall {
    NSLog(@"%s", __FUNCTION__);
    [[Common shareInstance]callWithPhoneString:onCall.view.accessibilityValue inView:self.view];
}

#pragma mark - Cell
- (void)cellSetup:(UITableViewCell*)cell index:(NSInteger)index;
{
    if (self.array_nearby.count <= 0) {
        return;
    }
    NSDictionary *cellDic = _array_nearby[index];
    NSString *imgUrl = [Common valueForKey:P_LOGO_PATH dic:cellDic];
    NSString *title = [Common valueForKey:P_NAME dic:cellDic];
    NSString *districtName = [Common valueForKey:P_V2_DISTRICT_NAME dic:cellDic];
    NSString *type_name = [Common valueForKey:P_LABEL dic:cellDic];
    NSString *good = [Common valueForKey:P_V2_GOOD dic:cellDic];
    NSString *comment_cnt = [Common valueForKey:P_V2_COMMENT_CNT dic:cellDic];
    NSString *base_price = [Common valueForKey:P_BASE_PRICE dic:cellDic];
    UIImageView *image = (UIImageView*)[cell viewWithTag:1001];
    [image setImageWithURL:[Common getFullDownloadPathByRelativePath:imgUrl] placeholderImage:[UIImage imageNamed:@"placeholder"]];
    UILabel *titleLab = (UILabel*)[cell viewWithTag:1002];
    //[titleLab setNumberOfLines:2];
    titleLab.text = title;
    //[titleLab sizeToFit];
    //CGRect frame = titleLab.frame;
    //frame.size.width = 216;
    //titleLab.frame = frame;
    [self tagSetup:cellDic titleLabHeight:-1 cell:cell];
    UILabel *price = (UILabel*)[cell viewWithTag:1005];
    if ([base_price isEqualToString:@""] || [base_price isEqualToString:@"免费"] || base_price == nil){
        [price removeFromSuperview];
    } else {
        NSArray *priceArray = [base_price componentsSeparatedByString:@"元"];
        price.text = [NSString stringWithFormat:@"%@%@",priceArray[0],@"元起"];
    }
    UILabel *districtNameLab = (UILabel*)[cell viewWithTag:2002];
    districtNameLab.text = [NSString stringWithFormat:@"    %@", districtName];
    UILabel* thirdLab = (UILabel*)[cell viewWithTag:2007];
    thirdLab.text = type_name;
    UILabel* goodLab = (UILabel*)[cell viewWithTag:2004];
    goodLab.text = good;
    UILabel* CommontLab = (UILabel*)[cell viewWithTag:2006];
    CommontLab.text = comment_cnt;
}

//标签描绘
- (void)tagSetup:(NSDictionary*)cellDic titleLabHeight:(CGFloat)height cell:(UITableViewCell*)cell
{
    NSArray *tagsList = [Common valueForKey:P_TAGS dic:cellDic];
    NSString *open_time = [Common valueForKey:P_OPEN_TIME dic:cellDic];
    UIView* tagListView = tagListView = [cell viewWithTag:1003];
    if(tagsList.count <= 0){
        UILabel *openTimeLab = (UILabel*)[cell viewWithTag:2008];
        openTimeLab.text = open_time;
    } else {
        CGFloat tagX = 0.0;
        int count = [tagsList count] > 3 ? 3 : [tagsList count];
        for (int i = 0; i < count; i++) {
            NSDictionary *tagDic = tagsList[i];
            NSString *cover_path = [Common valueForKey:P_COVER_PATH dic:tagDic];
            NSArray *rgbArray = [[Common valueForKey:P_V2_RGB dic:tagDic] componentsSeparatedByString:@"#"];
            if ([cover_path isEqualToString:@""]) {
                NSString *rgb = [NSString stringWithFormat:@"%@%@", @"0x",@"18aaf6"];
                if (rgbArray.count > 1) {
                    rgb = [NSString stringWithFormat:@"%@%@", @"0x",rgbArray[1]];
                }
                NSString *name = [Common valueForKey:P_NAME dic:tagDic];
                UIView *tagView = [[UIView alloc] initWithFrame:CGRectMake(tagX, 0, 45, 16)];
                tagView.layer.cornerRadius = 3.0;
                unsigned int color = strtoul([rgb UTF8String],0,0);//nsstring 转 rgb
                tagView.backgroundColor = [Common colorWithRGB:color];
                UILabel *tagNameLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tagView.frame.size.width, tagView.frame.size.height)];
                tagNameLab.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:10.0];
                tagNameLab.textAlignment = UITextAlignmentCenter;
                tagNameLab.textColor = [UIColor whiteColor];
                tagNameLab.text = name;
                [tagNameLab sizeToFit];
                CGRect frame = tagNameLab.frame;
                frame.size.width = MAX(tagNameLab.frame.size.width + 5, 45);
                frame.size.height = 16;
                frame.origin = tagView.frame.origin;
                tagView.frame = frame;
                frame.origin = tagNameLab.frame.origin;
                tagNameLab.frame = frame;
                [tagView addSubview:tagNameLab];
                [tagListView addSubview:tagView];
                tagX += tagView.frame.size.width + 5;
            } else {
                UIImageView *tagsImg = [[UIImageView alloc]initWithFrame:CGRectMake(tagX, 0, 12, 12)];
                [tagsImg setImageWithURL:[Common getFullDownloadPathByRelativePath:cover_path]];
                [tagListView addSubview:tagsImg];
                tagX += 20;
            }
        }
    }
}

@end
